﻿using System;
using System.Collections.Generic;
using System.Text;
using CGLib.Enums;

namespace CGLib
{
    /// <summary>
    /// MSN packet information.
    /// </summary>
    public class MSNPacket
    {
        private MsgType msgtype;
        private string[] info;

        public MSNPacket(string message, out bool successful)
        {
            // Check if the message starts with the MSN game command prefix
            if (!message.StartsWith("@TLC ")) goto SKIPPARSE;

            string[] strs = message.Split(' ');
            msgtype = GetCommand(strs[1]);

            // If the command cannot be recognised.
            if (msgtype == (MsgType)(-1))
                goto SKIPPARSE;

            // If there are more than two pairs of information (the standard is @TLC XXX),
            // put this into the info string array.
            if (strs.Length > 2)
            {
                info = new string[strs.Length - 2];

                for (int i = 0; i < info.Length; i++)
                    info[i] = strs[i + 2];
            }

            // Successful parse.
            successful = true;
            return;

            // If the message sent is not a MSN game packet.
        SKIPPARSE:
            successful = false;
            return;
        }

        /// <summary>
        /// The type of packet message.
        /// </summary>
        public MsgType Type
        {
            get { return msgtype; }
            set { msgtype = value; }
        }

        /// <summary>
        /// The information of the packet.
        /// </summary>
        public string[] Info
        {
            get { return info; }
            set { info = value; }
        }

        /// <summary>
        /// Get the type of command the message has received/sent.
        /// </summary>
        /// <param name="value">The command value</param>
        private static MsgType GetCommand(string value)
        {
            switch (value)
            {
                case "INV": return MsgType.Invite;
                case "RDY": return MsgType.Ready;
                case "MSG": return MsgType.Announce;
                case "IFC": return MsgType.CardInfo;
                case "FLD": return MsgType.FieldCards;
                case "CUS": return MsgType.Custom;
            }

            return (MsgType)(-1);
        }
    }
}
